<?php
///usr/local/php/bin/php -c /usr/local/php/etc/php.ini /data/www/esmj_script/script.php request/CleanClbHandler
class CleanClbHandler extends ScriptBase
{
    private $_redis;
    private $optime;

    /**
     * 清除过期的CLB俱乐部数据
     */
	public function logic()
	{
	    set_time_limit(0);
	    $this->optime = time() - 7*24*3600;
        
        // 清除UPZ过期数据
        $this->_redis = $this->redis('paiju_log',5);
        $min = 1; 
        $rds = $this->redis('room_status',8);
        $max = intval($rds->get('clubSeedID'));
        
        $total = 0;
        $j = 1;
        for ($i=$min;$i<=$max;$i++){
            if( $j % 1000 == 0){
                //file_put_contents('./clb.log', date('Y-m-d H:i:s')." ".($i-1000)." - {$i}".PHP_EOL, FILE_APPEND);
                usleep(100000);
            }
            $clubid = (10*$i*$i + $i) % 100000 + 100000;
            $clubkey = 'club:'.$clubid;
            if($rds->exists($clubkey)){
                echo $clubkey."\n";
                $clb = $rds->hGet('club:'.$clubid,'members');
                $clbm = json_decode($clb,true);
                if(!empty($clbm) && count($clbm) > 0){
                    $this->trim_data('PZCLB:'.$clubid);
                    foreach ($clbm as $val){
                        $this->trim_data("UPZCLB:{$clubid}_{$val[0]}");
                    }
                }
            }
            $j++;
        }
        
        $this->deinitRedis('paiju_log');
        $this->deinitRedis('room_status');
	}
    
	//截取数据
    private function trim_data($opkey){
        if(!$this->_redis->exists($opkey)){
            return 0;
        }
        //echo $opkey."\n";
        
        $i = 1;
        $step = 10;
        $index = 0;
        while (true) {
            $index = $step * $i;
            $center = $this->_redis->lindex($opkey,$index);
            if(empty($center)) break;
            $ctime = intval(substr($center, 0,10));
            if($ctime <= $this->optime){
                //echo $opkey."---".$ctime."\n";
                break ;
            }
            $i ++;
            //usleep(10000);
        }
        $this->_redis->ltrim($opkey,0,$index);
        usleep(10000);
    }
}